<?php

require_once dirname(__FILE__).'/../lib/communicationGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/communicationGeneratorHelper.class.php';

/**
 * communication actions.
 *
 * @package    tfe
 * @subpackage communication
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class communicationActions extends autoCommunicationActions
{

    // surcharge pour inscription rapide
    public function executeNew(sfWebRequest $request) {
        $id = $request->getParameter('id');
        if($id) {
            $ins = new Communication();
            $ins->setIdContact($request->getParameter('id'));
            $this->form = new CommunicationForm($ins);
        }else {
            $this->form = $this->configuration->getForm();
        }
        $this->communication = $this->form->getObject();
    }


  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

    if($form->getValue('id_contact') != null && $form->getValue('id_groupe') != null){
        $this->getUser()->setFlash('error','Choisissez un contact ou un groupe mais pas les deux !',false);
    }else{
        if ($form->isValid())
        {
          $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

          $communication = $form->save();

          $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $communication)));

          if ($request->hasParameter('_save_and_add'))
          {
            $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

            $this->redirect('@communication_new');
          }
          else
          {
            $this->getUser()->setFlash('notice', $notice);

            $this->redirect(array('sf_route' => 'communication_edit', 'sf_subject' => $communication));
          }
        }
        else
        {
          $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
     }
  }

    // utile pour que les actions batch respecten le softDelete
    protected function executeBatchDelete(sfWebRequest $request) {
        $ids = $request->getParameter('ids');

        $records = Doctrine_Query::create()
                ->select()
                ->from('Communication')
                ->whereIn('id', $ids)
                ->execute();

        $count = 0;
        foreach($records as $record){
            $count += $record->delete();
        }

        if ($count >= count($ids)) {
            $this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.');
        }
        else {
            $this->getUser()->setFlash('error', 'A problem occurs when deleting the selected items.');
        }

        $this->redirect('@communication');
    }
}
